//
//  NSDate+Friend.m
//  WeiXiao
//
//  Created by xxxxx on 13-4-10.
//
//

#import "NSDate+Friend.h"

@implementation NSDate (Friend)
/* * 当天内的：1分钟内，显示"刚刚"，1小时内的，直接显示"多少分钟前",如"15分钟前"；1小时外的，直接显示"多少小时前"，如"3小 时前"
 * 昨天的：直接显示"昨天上下午或晚上"
 * 本周的：直接显示"周几上下午或晚上"
 * 其他：如果是当年，则显示"MM-DD"，否则显示"YYYY-MM-DD"
 */

#pragma mark -- Date & TimeInertval
- (NSInteger)timeIntervalNow{
    NSDate *now = [NSDate date];
    NSTimeInterval v = [now timeIntervalSinceDate:self];
    return v;
}
- (NSInteger)timeIntervalNowGMT{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interver = [timeZone secondsFromGMTForDate:self];
    return [self timeIntervalSince1970] - interver;
}
+ (NSDate *)dateFromTimeInterver:(NSInteger)timeInterver{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970: timeInterver];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interver = [timeZone secondsFromGMTForDate: [NSDate date]];
    date = [date dateByAddingTimeInterval: + interver];
    return date;
}

+ (NSInteger)dateStringToTimeInterver:(NSString *)dateString{
    NSDate *date = nil;
    if (dateString.length == 10) {
        date = [NSDate DateFromShortDate:dateString];
    } else {
        date = [NSDate DateFromFullTime:dateString];
    }
    return [date timeIntervalSince1970];
}

+ (NSInteger)dateStringToTimeInterverGMT:(NSString *)dateString{
    NSDate *date = nil;
    if (dateString.length == 10) {
        date = [NSDate DateFromShortDate:dateString];
    } else {
        date = [NSDate DateFromFullTime:dateString];
    }
    return [date timeIntervalSince1970];
}

#pragma mark -- Date To String
- (NSString *)formatterDate:(NSString *)formatter{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:formatter];
    NSString *s = [fmt stringFromDate:self];
    return s;
}

- (NSString *)formatterGMTDate:(NSString *)formatter{
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setTimeZone:GTMzone];
    [fmt setDateFormat:formatter];
    NSString *s = [fmt stringFromDate:self];
    return s;
}

#pragma mark -- Date Unit
- (NSInteger)year{
    NSString *a = [self formatterDate:@"yyyy"];
    return [a integerValue];
}
- (NSInteger)month{
    NSString *a = [self formatterGMTDate:@"MM"];
    return [a integerValue];
}
-(NSInteger)day{
    NSString *a = [self formatterGMTDate:@"dd"];
    return [a integerValue];
}
- (NSInteger)hour{
    NSString *a = [self formatterGMTDate:@"HH"];
    return [a integerValue];
}
- (NSInteger)minute{
    NSString *a = [self formatterGMTDate:@"mm"];
    return [a integerValue];
}
- (NSInteger)second{
    NSString *a = [self formatterGMTDate:@"ss"];
    return [a integerValue];
}
- (NSInteger)numberOfDaysInMonth{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit: NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate: self];
    NSInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

#pragma mark -- String To Date
+ (NSDate *)DateFromFullTime:(NSString *)timeString{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [fmt setTimeZone:GTMzone];
    [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [fmt dateFromString:timeString];
    return date;
}
+ (NSDate *)DateFromShortDate:(NSString *)timeString{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [fmt setTimeZone:GTMzone];
    [fmt setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [fmt dateFromString:timeString];
    return date;
}
+ (NSDate *)dateForLocalTimeZone:(NSString *)timeString formatterString:(NSString *)formatterString{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: formatterString];
    NSDate *date = [formatter dateFromString: timeString];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interver = [timeZone secondsFromGMTForDate: [NSDate date]];
    date = [date dateByAddingTimeInterval: interver];
    return date;
}
+ (NSDate *)dateForLocalFromFullDate:(NSString *)dateString{
    return [self dateForLocalTimeZone: dateString formatterString: @"yyyy-MM-dd HH:mm:ss"];
}
+ (NSDate *)dateForLocalFromShortDate:(NSString *)dateString{
    return [self dateForLocalTimeZone: dateString formatterString: @"yyyy-MM-dd"];
}
@end
